import { defHttp } from '/@/common/util/axios';
import { useMessage } from '/@/hooks/web/useMessage';

const { createConfirm } = useMessage();

enum Api {
  list = '/emp/hubTeamEvaluateIndex/getPage',
  save = '/emp/hubTeamEvaluateIndex/add',
  edit = '/emp/hubTeamEvaluateIndex/edit',
  deleteOne = '/emp/hubTeamEvaluateIndex/delete',
  importExcel = '/emp/hubTeamEvaluateIndex/importExcel',
  exportXls = '/emp/hubTeamEvaluateIndex/exportXls',
  deleteBatch = '/emp/hubTeamEvaluateIndex/deleteBatch', //批量删除
  updateBatch = '/emp/hubTeamEvaluateIndex/updateBatch', //批量新增或修改
  treeList = '/emp/hubTeamTemplate/getList', // 获取模板树结构
  getTemplateConfigHeader = '/emp/dynamicTable/getTemplateConfigHeader', // 动态表头-模版配置
  getPeriodTree = '/emp/epmBasePeriod/periodTree', //考核周期树,
  templateConfigList = '/emp/hubTeamTemplateConfig/getList', // 模板配置列表
  saveTemplateConfigs = '/emp/hubTeamTemplateConfig/updateBatch',
}

/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;

/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;

// 树形列表接口
export const treeList = (params) => defHttp.get({ url: Api.treeList, params });

// 模板配置列表
export const getTemplateConfigList = (params) => defHttp.get({ url: Api.templateConfigList, params });

// 模板配置保存
export const saveConfigs = (params) => defHttp.post({ url: Api.saveTemplateConfigs, params });

export const getPeriodTree = () => {
  const params = {
    type: 'year',
  };
  return new Promise((resolve) => {
    defHttp.post({ url: Api.getPeriodTree, params }, { joinParamsToUrl: true }).then((res) => {
      resolve(res);
    });
  });
};

// 获取动态表头
export const getTemplateConfigHeader = (params) => defHttp.post({ url: Api.getTemplateConfigHeader, params });

/**
 * 列表接口
 * @param params
 */
export const pageList = (params) => defHttp.get({ url: Api.list, params });

/**
 * 删除单个
 */
export const deleteOne = (params, handleSuccess) => {
  return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
    handleSuccess();
  });
};

/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
        handleSuccess();
      });
    },
  });
};

/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  const url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({ url: url, params });
};

/**
 * 批量更新
 * @param params
 */
export const updateList = (params) => {
  const url = Api.updateBatch;
  return defHttp.post({ url: url, params });
};
